package com.home.job.zh1.job3;


import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MaximumSubsequence {

    private static List<Integer> data = new ArrayList<>();

    public static int maxProduct(int n) {
        //如果数组为null或者数组长度为0，直接返回0.
        if (null == data || data.size() == 0) {
            return 0;
        }

        //开始将A[0]分别设置为：最大值、最小值，和最终的返回结果。
        int max = 0;
        int result = 0;

        //从数组的第二个元素开始取值
        for (int i = 0; i < data.size() - n; i++) {
            max = 1;
            for (int j = 0; j < n; j++) {
                max *= data.get(i + j);
            }

            if (max > result) {
                result = max;
            }
        }
        return result;
    }

    public static void initList() {
        try {
            String textUrl = System.getProperty("user.dir") + "\\src\\com\\home\\job\\zh1\\job3\\data.txt";
            File file = new File(textUrl);
            if(file.isFile() && file.exists()) {
                InputStreamReader isr = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);
                String lineTxt = null;
                while ((lineTxt = br.readLine()) != null) {
                    for (int i = 0; i < lineTxt.length(); i++) {
                        data.add(Integer.parseInt(String.valueOf(lineTxt.charAt(i))));
                    }
                }
                br.close();
            } else {
                System.out.println("文件不存在!");
            }
        } catch (Exception e) {
            System.out.println("文件读取错误!");
        }
    }

    public static int getInt() {
        try {
            Scanner sc = new Scanner(System.in);
            return sc.nextInt();
        }catch(Exception e){
            System.out.println("请输入正确数字序号！");
            return 0;
        }
    }

    public static void main(String[] args) throws IOException {
        initList();
        System.out.println("请输入n: ");
        System.out.println("RESULT IS: " + maxProduct(getInt()));
    }


}
